package com.amat0urheu.controller.adminController;

import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.pojo.CloudPojo.NetWork;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.CloudService;
import com.amat0urheu.service.cloudService.NetWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class NetWrokController {
    @Autowired
    CloudService cloudService;
    @Autowired
    NetWorkService netWorkService;
    @PreAuthorize("hasAuthority('ROLE_admin')")
   @GetMapping("/admin/cloud/getAllNetWorks/{openstackIP}/{pageSize}/{pageNum}")
   ReturnRes getAllNetWorks(@PathVariable String openstackIP, @PathVariable Integer pageSize, @PathVariable Integer pageNum) {

       return netWorkService.getAllNetWorks(openstackIP, pageNum, pageSize);
   }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="1",action="刷新网路")
   @PostMapping("/admin/cloud/refreshNetwork")
   ReturnRes refreshNetwork(@RequestParam(name = "openstackIP") String openstackIP) {
       netWorkService.refreshNetwork(openstackIP);
       return new ReturnRes(true, "刷新成功");
   }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="创建网络")
    @PostMapping("/admin/cloud/createNetwork")
    public ReturnRes createNetWork(@RequestParam(name = "data") String netWorkJson) {
        NetWork netWork = JSONObject.parseObject(netWorkJson, NetWork.class);
        return netWorkService.createNetwork(netWork);

    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="删除网络")
    @PostMapping("/admin/cloud/removeNetwork")
    public ReturnRes removeNetwork(@RequestParam(name = "data") String netWorkJson) {
        NetWork netWork = JSONObject.parseObject(netWorkJson, NetWork.class);

        return netWorkService.removeNetWork(netWork);
    }
}